package com.zl.smartmall.library.po;

import java.util.ArrayList;

import net.tsz.afinal.annotation.sqlite.Id;
import net.tsz.afinal.annotation.sqlite.Property;
import net.tsz.afinal.annotation.sqlite.Table;
import net.tsz.afinal.annotation.sqlite.Transient;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.zl.smartmall.library.util.CrashHandler;

@Table(name = "tb_product_model")
public class BrandedProductsInfo {    
	@Id
	private int id;
	@Property
	private int activitiesId;
	@Property
	private int brandId;
	@Property
	private int inventoryNum;//库存
	@Property
	private int productId;
    @Property
    private int shopId;
    @Property
    private int saleNum;//销售量(历史总销量)
	@Property
	private double originalCost;
	@Property
	private double currentCost;
	@Property
	private String productName;
	@Property
	private String productIcon;
	@Property
	private String productIntro;
    @Property
    private String shopName;
    @Property
    private String brandName;
    @Property
    private String openTime;
    @Property
    private String labelString;
    @Transient
    private String[] labels;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getActivitiesId() {
		return activitiesId;
	}

	public void setActivitiesId(int activitiesId) {
		this.activitiesId = activitiesId;
	}

	public int getBrandId() {
		return brandId;
	}

	public void setBrandId(int brandId) {
		this.brandId = brandId;
	}

	public double getOriginalCost() {
		return originalCost;
	}

	public void setOriginalCost(double originalCost) {
		this.originalCost = originalCost;
	}

	public double getCurrentCost() {
		return currentCost;
	}

	public void setCurrentCost(double currentCost) {
		this.currentCost = currentCost;
	}

	public String getProductName() {
		return productName;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

	public String getProductIcon() {
		return productIcon;
	}

	public void setProductIcon(String productIcon) {
		this.productIcon = productIcon;
	}

	public String getProductIntro() {
		return productIntro;
	}

	public void setProductIntro(String productIntro) {
		this.productIntro = productIntro;
	}

	public int getInventoryNum() {
		return inventoryNum;
	}

	public void setInventoryNum(int inventoryNum) {
		this.inventoryNum = inventoryNum;
	}

	public int getProductId() {
		return productId;
	}

	public void setProductId(int productId) {
		this.productId = productId;
	}

	public int getShopId() {
		return shopId;
	}

	public void setShopId(int shopId) {
		this.shopId = shopId;
	}

	public int getSaleNum() {
		return saleNum;
	}

	public void setSaleNum(int saleNum) {
		this.saleNum = saleNum;
	}

	public String getShopName() {
		return shopName;
	}

	public void setShopName(String shopName) {
		this.shopName = shopName;
	}   
	
	public String getBrandName() {
		return brandName;
	}

	public void setBrandName(String brandName) {
		this.brandName = brandName;
	}

	public String getOpenTime() {
		return openTime;
	}

	public void setOpenTime(String openTime) {
		this.openTime = openTime;
	}

	public String getLabelString() {
		return labelString;
	}

	public void setLabelString(String labelString) {
		this.labelString = labelString;
	}

	public String[] getLabels() {
		return labels;
	}

	public void setLabels(String[] labels) {
		this.labels = labels;
	}
	
	public void decode() {
		try {
			if(labelString != null) {
				JSONArray jsonArray = new JSONArray(labelString);
				labels = new String[jsonArray.length()];
				for(int i = 0; i < jsonArray.length(); i++) {
					labels[i] = jsonArray.getString(i);
				}
			}
		} catch(Exception e) {
			CrashHandler.e(e);
		}
	}
	
	public void encode() {
		if(labels != null) {
			JSONArray jsonArray = new JSONArray();
			for(String s : labels) {
				jsonArray.put(s);
			}
			labelString = jsonArray.toString();
		}else {
			labelString = "";
		}
	}

	private static BrandedProductsInfo parse(JSONObject jsonObject) throws JSONException{
		if(jsonObject == null){
			return null;
		}
		
		BrandedProductsInfo data = new BrandedProductsInfo();
		data.brandId = jsonObject.getInt("brand_id");
		data.shopId = jsonObject.getInt("shop_id");
        data.shopName = jsonObject.getString("shop_name");
        data.productId = jsonObject.getInt("product_id");
        data.productIntro = jsonObject.getString("product_title");
        data.productIcon = jsonObject.getString("pic");
        data.originalCost = jsonObject.getDouble("tag_price");
        data.currentCost = jsonObject.getDouble("sale_price");
        data.saleNum = jsonObject.getInt("sale_num");
        data.inventoryNum = jsonObject.getInt("num");
        data.brandName = jsonObject.getString("brand_name");
        data.openTime = jsonObject.getString("open_time");
        JSONArray labelArray = jsonObject.getJSONArray("promotion");
        data.labels = new String[labelArray.length()];
        for(int i = 0; i < labelArray.length(); i++) {
        	data.labels[i] = labelArray.getJSONObject(i).getString("name");
        }
		
		return data;
	}
	
    public static ArrayList<BrandedProductsInfo> parseArray(JSONArray jsonArray, int activitiesId) throws JSONException{
		ArrayList<BrandedProductsInfo> datas = new ArrayList<BrandedProductsInfo>();
		
		if(jsonArray == null || jsonArray.length() == 0){
			return datas;
		}
		
		for(int i = 0; i < jsonArray.length(); i++){
			BrandedProductsInfo data = parse(jsonArray.getJSONObject(i));
			if(data != null){
				data.activitiesId = activitiesId;
				datas.add(data);
			}
		}
		return datas;
		
	}
}
